The DoCreateTPool function in Listing 1 creates a pool of threads. While it isn't strictly necessary to create a pool of threads--you can create and allocate threads in one step with the NewThread function--there are advantages to doing so. For example, you can allocate all the memory for your threads up front before memory is used or fragmented. Listing 2 shows the code in DoCreateTPool , which creates a pool of threads.
Listing 2 Creating a thread pool
#define kNumOfPhilos 5 /* Number of philosopher icons*/
#define kDefaultStackSize 0 /* System determines stack size */
void DoCreateTPool()
{
OSErr anError;
/* Make a pool of threads for the philosophers */
anError = CreateThreadPool(kCooperativeThread,
kNumOfPhilos, kDefaultStackSize);
if ( anError )
DoHandlerError ("\pProblem creating thread pool",
anError, kFatal);
}
The code in DoCreateTPool passes three parameters to the CreateThreadPool function. The first, kCooperativeThread , is a constant defined by the Thread Manager specifying that the threads to create are cooperative threads.
Note
Historically, the Thread Manager supported two types of threads, cooperative and preemptive but now only cooperative threads are supported. The CreateThreadPool function (and the NewThread function) still require that you specify the type of the thread, even though only one type is available.
The next parameter, kNumOfPhilos , is an application-defined constant that specifies the number of threads to create--in this case, five. The last parameter, kDefaultStackSize , specifies that Thread Manager use the default stack size for the five threads that it creates. You can specify the size in bytes if you don't wish to use the default size. The Thread Manager defines a default size that is probably larger than the minimum size that is required.
If there is a problem creating the threads, DoCreateTPool calls the error handling function and passes it the result code returned by CreateThreadPool . Note that if there is not enough memory to create all the specified threads, CreateThreadPool creates none and returns the memFullErr result code.